﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;//
namespace MoPhongThuatToan
{
    class Euler
    {
        List<int> a = new List<int>();
        private int nE;
	    private int[,] L=new int[100,100];
        public void NhapFile(String FilePath)
        {
            FileStream fs = new FileStream(FilePath, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            nE = Convert.ToInt32(sr.ReadLine());
            for (int i = 0; i < nE; i++)
            {
                string Line = sr.ReadLine();
                string[] G = Line.Split(new char[] { ' ' });
                for (int j = 0; j < nE; j++)
                {
                    L[i, j] = int.Parse(G[j]);
                }
            }
            sr.Close();
            fs.Close();
        }
        public void XuatFile(string FilePath)
        {
            FileStream fs = new FileStream(FilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            int kq = KTCTEuler();
            if (kq == 1)
            {
                //sw.Write("Chu trinh euler: \n");
                for (int i = a.Count-1; i >= 0; i--)
                {
                    sw.Write(a[i] + 1);
                    sw.Write("\t");
                }
            }
            sw.Close();
            fs.Close();
        }
        public void TimDuong(int i,List<int> a1)
        {
	        for(int j=0;j<nE;j++)
	        {
		        if(L[i,j]!=0)
		        {
			        L[i,j]=L[j,i]=0;
			        TimDuong(j,a1);
		        }
	        }
            a1.Add(i);
        }
        public int KTCTEuler()
        {
	        int x=0;
	        int co=0;
	        for(int i=0;i<nE;i++)
	        {
		        for(int j=0;j<nE;j++)
		        {
			        if(L[i,j]==1)
			        {
				        x=i;
				        co=1;
				        break;
			        }
		        }
		        if(co==1)
			        break;
	        }
	        Euler e = this;            
	        e.TimDuong(x,a);
	        for(int i=0;i<e.nE;i++)
	        {
		        for(int j=0;j<e.nE;j++)
		        {
			        if(e.L[i,j]!=0)
				        return 0;
		        }
	        }
            if(e.a[e.a.Count-1]!=e.a[0])
		        return 0;
	        return 1;
        }
    }
}
